import socket
import threading

def start_client():
    """连接公网服务器并映射内网服务"""
    control_conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    control_conn.connect(('public-server-ip', 7000))  # 替换为实际公网 IP
    print("Connected to server on :7000")

    while True:
        data = control_conn.recv(1024)
        if not data:
            break

        # 连接内网服务
        local_conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        local_conn.connect(('127.0.0.1', 8080))  # 内网服务地址
        local_conn.send(data)

        # 双向转发
        threading.Thread(target=forward, args=(local_conn, control_conn)).start()
        forward(control_conn, local_conn)

    control_conn.close()

def forward(src, dst):
    """数据转发"""
    try:
        while True:
            data = src.recv(1024)
            if not data:
                break
            dst.send(data)
    except:
        pass
    finally:
        src.close()

if __name__ == "__main__":
    start_client()